import { createRouter, createWebHistory } from 'vue-router'

// 引入组件
import Config from '@/views/micro/Config.vue'
import Result from '@/views/micro/Result.vue'
import ReverseShConfig from "@/views/reversh/ReverseShConfig.vue";
import ReverseShAuditResult from "@/views/reversh/ReverseShAuditResult.vue";
import { useNavStore } from "@/store/nav.ts";
import Multi from "@/views/cluster/multi/Multi.vue";
import BasicHome from "@/views/BasicHome.vue";
import FlowList from "@/views/ddos/FlowList.vue";
import ClusterTopology from '@/views/cluster/multi/Topology.vue';
import Topology from '@/views/cluster/multi/Topology.vue';
import ZtrustScore from "@/views/ztrust/ZtrustScore.vue";
import ZtrustEvent from "@/views/ztrust/ZtrustEvent.vue";

const router = createRouter({
    history: createWebHistory(), // 路由器的工作模式
    routes: [
        {
            path: '/cluster',
            component: Multi,
            name: 'cluster',
            meta: {
                title: '边云集群信息',
                // 这里要设置为false，只有子路由才应该触发集群视图
                isClusterView: false
            },
            // 修改子路由配置
            children: [
                {
                    // 当访问 /cluster/:clusterId 时，重定向到概览页
                    path: ':clusterId',
                    redirect: to => ({
                        name: 'cluster-overview',
                        params: { clusterId: to.params.clusterId }
                    }),
                },
                {
                    path: ':clusterId/overview',
                    name: 'cluster-overview',
                    component: () => import('@/views/cluster/ClusterMetaView.vue'),
                    meta: {
                        title: '集群概览',
                        isClusterView: true
                    }
                },
                {
                    path: ':clusterId/resource',
                    name: 'cluster-resource',
                    children: [
                        {
                            path: 'pod',
                            name: 'cluster-resource-pod',
                            component: () => import('@/views/cluster/resource/PodView.vue'),
                            props: true, // 将路由参数作为 props 传递
                            meta: {
                                title: 'Pod资源',
                                isClusterView: true
                            }
                        },
                        {
                            path: 'deployment',
                            name: 'cluster-resource-deployment',
                            props: true, // 将路由参数作为 props 传递
                            component: () => import('@/views/cluster/resource/DeploymentView.vue'),
                            meta: {
                                title: "Deployment资源",
                                isClusterView: true
                            }
                        },
                        {
                            path: 'service',
                            name: 'cluster-resource-service',
                            props: true, // 将路由参数作为 props 传递
                            component: () => import('@/views/cluster/resource/ServiceView.vue'),
                            meta: {
                                title: "Service资源",
                                isClusterView: true
                            }
                        }
                    ],
                    meta: {
                        title: '集群资源',
                        isClusterView: true
                    }
                },
                {
                    path: ':clusterId/scheduling',
                    name: 'cluster-scheduling',
                    component: () => import('@/views/cluster/SchedulingView.vue'),
                    props: true,
                    meta: {
                        title: '调度详情',
                        isClusterView: true
                    }
                }
            ]
        },
        {
            path: '/cluster-topology',
            name: 'cluster-topology',
            component: Topology,
            meta: {
                title: '集群拓扑'
            }
        },
        {
            path: '/basestation',
            name: 'basestation',
            meta: {
                title: '基站节能模块'
            },
            children: [
                {
                    path: 'example',
                    component: () => import('@/views/basestation/BaseOverview.vue'),
                    name: 'basestation-example',
                    meta: {
                        title: '示例'
                    }
                },
                {
                    path: 'geo',
                    component: () => import('@/views/basestation/BaseGeo.vue'),
                    name: 'basestation-geo',
                    meta: {
                        title: '地理分布'
                    }
                }
            ]
        },
        {
            path: '/security',
            name: 'security',
            meta: {
                title: '集群防护'
            },
            children: [
                {
                    name: 'micro',
                    path: 'micro',
                    meta: {
                        title: '微服务调用链'
                    },
                    children: [
                        {
                            name: 'micro-pattern-config',
                            path: 'pattern/config',
                            component: Config,
                            meta: {
                                title: '配置'
                            }
                        },
                        {
                            name: 'pattern/match',
                            path: 'pattern/match',
                            component: Result,
                            meta: {
                                title: '追踪'
                            }
                        }
                    ]
                },
                {
                    name: 'reversesh',
                    path: 'reversesh',
                    meta: {
                        title: '反弹shell'
                    },
                    children: [
                        {
                            name: 'result',
                            path: 'result',
                            component: ReverseShAuditResult,
                            meta: {
                                title: '审计结果'
                            }

                        }
                    ]
                },
                {
                    path: 'ddos',
                    name: 'ddos',
                    meta: {
                        title: 'DDoS攻击检测'
                    },
                    children: [
                        {
                            name: 'ddos-flowDetection',
                            path: 'flowDetection',
                            component: FlowList,
                            meta: {
                                title: '流量检测'
                            }
                        }
                    ]
                },
                {
                    name: 'ztrust',
                    path: 'ztrust',
                    meta: {
                        title: '零信任'
                    },
                    children: [
                        {
                            path: 'score',
                            component: ZtrustScore,
                            name: 'ztrust-score',
                            meta: {
                                title: '信任评级'
                            }
                        },
                        {
                            path: 'event',
                            component: ZtrustEvent,
                            name: 'ztrust-event',
                            meta: {
                                title: '信任事件'
                            }
                        },
                    ]
                }
            ]
        },
        {
            name: 'services',
            path: '/services',
            meta: {
                title: '服务管理'
            },
            children: [
                {
                    name: 'services-display',
                    path: 'display',
                    component: () => import('@/views/services/Display.vue'),
                    meta: {
                        title: '服务列表'
                    }

                }
            ]
        },
        {
            // 首页路径
            path: '/',
            name: 'home',
            component: BasicHome,
        }
    ],
})

router.beforeEach((to) => {
    // 每次进入到新的路由时候，设置pinia中的面包屑导航信息
    const navStore = useNavStore();
    navStore.breadcrumbLevel = [];

    // 检查当前路由是否是集群视图
    // const isClusterView = to.matched.some(record => record.meta.isClusterView);
    const isClusterView = to.meta.isClusterView
    // 设置导航视图状态
    if (isClusterView) {
        let clusterId = to.params.clusterId;
        navStore.setClusterView(clusterId);
    } else {
        navStore.setDefaultView();
    }

    for (const route of to.matched) {
        if (route.meta.title) {
            // 有components属性才可以点击跳转，否则changeRouteFunc啥都不做
            let changeRouteFunc = route.components ? async () => {
                await router.push(route.path); // 添加导航跳转功能
            } : async () => {
            };

            navStore.breadcrumbLevel.push({
                title: route.meta.title as string,
                value: route.path,
                changeRoute: changeRouteFunc, // 添加导航跳转功能
            });
        }
    }
})

// 修正导出方式 - 使用默认导出
export default router;
